package com.chatgpt.platformbasedchatgpt.controller;

import io.github.asleepyfish.util.OpenAiUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@CrossOrigin(origins = "http://127.0.0.1:88")
public class ChatGPTController {
    @GetMapping("/Chat")
    public void Chat(@RequestParam(required = false) String message, HttpServletResponse response) throws IOException {
        // 需要指定response的ContentType为流式输出，且字符编码为UTF-8
        response.setContentType("text/event-stream");
        response.setCharacterEncoding("UTF-8");
        // 禁用缓存
        response.setHeader("Cache-Control", "no-cache");
        System.out.println("1");
        OpenAiUtils.createStreamChatCompletion(message, response.getOutputStream());
    }
}
